//
// Created by 33145 on 2023/6/1.
//

#ifndef ROCKET_TCP_BUFFER_H
#define ROCKET_TCP_BUFFER_H

#include <memory>
#include <vector>

namespace rocket {
  class TCPBuffer {
  public:
    typedef std::shared_ptr<TCPBuffer> ptr;
    explicit TCPBuffer(int size);
    ~TCPBuffer() = default;

    // 返回可读字节数
    int readAble() const;
    // 返回可写的字节数
    int writeAble();

    int readIndex() const;

    int writeIndex() const;

    int writeToBuffer(const char* buf, int size);

    int readFromBuffer(std::vector<char>& re, int size);

    void resizeBuffer(int new_size);

    void adjustBuffer();

    void moveReadIndex(int size);

    void moveWriteIndex(int size);

    std::vector<char>& getBuffer() { return _buffer; }

  private:
    int _read_index {0};
    int _write_index {0};
    int _size {0};

    std::vector<char> _buffer;
  };
}

#endif //ROCKET_TCP_BUFFER_H
